import { MemberConst } from "src/consts/member_const";
import { PopupName } from "src/consts/popup_const";
import * as Api from 'src/api/net';

const state: any = {};
// StateIniter.init(state, 'demo');

state.formData = {};
state.Authority = []
state.id = ""
state.name = ""
state.status = ""
state.remark = ""
state.feature_code = ""

// vuex

/** 更新数据的 */
const mutations = {
    [MemberConst.CommitMemberShowList](state: any, data: any): void {
        console.log("Member_Panel data update", data);
        state.id = data.id
        state.name = data.name
        state.status = data.status
        state.remark = data.remark
        state.feature_code = data.feature_code
        state.Authority = []
        var arr = ["ROLE_assist_start",
            "ROLE_assist_operate_request",
            "ROLE_assist_webhistory_add",
            "ROLE_assist_webaccount_get",
            "ROLE_assist_task_close",
            "ROLE_assist_config_get",
            "ROLE_assist_operate_result_get",
            "ROLE_assist_camsgpin_get",
            "ROLE_assist_website_get",
            "ROLE_assist_cookie_get",
            "ROLE_option_list"]

        for (let i = 0; i < data.authority_info_list.length; i++) {
            var flag = true
            for (let k = 0; k < arr.length; k++) {
                if (data.authority_info_list[i].id == arr[k]) {
                    flag = false
                }
            }
            if (flag) {
                state.Authority.push(data.authority_info_list[i])
            }
        }

        if (WLStore().state.member.type == "edit") {
            WLLib.panel(PopupName.Member_Panel);
        } else {
            WLLib.panel(PopupName.Member_Set_Panel).close();
        }
    },
    [MemberConst.CommitAuthority](state: any, data: any): void {
        state.Authority = data
        WLLib.panel(PopupName.Member_Set_Panel).close();
    }
}

//异步处理
const actions = {
    [MemberConst.DispatchMemberShow](context: VuexData, data?: any): void {
        Api.AjaxPost(Config.ajaxUrl + "/role/show.do", data, (res: any) => {
            if (isNoEmptyObject(res.data)) {
                WLCommit(MemberConst.CommitMemberShowList, res.data);
            }
        })
    },
    [MemberConst.DispatchMemberEdit](context: VuexData, data?: any): void {
        Api.AjaxPost(Config.ajaxUrl + "/role/edit.do", data, (res: any) => {
            WLMessage("编辑角色成功", "success")
            let senData: any = {}
            senData["curr_page"] = WLStore().state.member.curr_page
            senData["page_size"] = WLStore().state.member.page_size
            WLDispatch(MemberConst.DispatchMemberList, senData);
            WLLib.panel(PopupName.Member_Panel).close();

            if (WLStore().state.member.type == "set") {
                WLLib.panel(PopupName.Member_Set_Panel).close();
            }
        })
    }
}

export default {
    state,
    mutations,
    actions
}